package de.fhd.pms.ctrl;

import java.util.Date;
import java.util.Map;

import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import de.fhd.pms.dao.*;
import de.fhd.pms.model.*;
/**
 * The controller class for User.
 */
@Controller
public class UserController {
	private static Logger log = Logger.getLogger(UserController.class);
	private UserDao userDao = null;

	/**
	 * Set UserDao
	 */
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	/**
	 * In case of Requesting "/registertotour.html" & RequestMethod = POST,
	 * the Controller opens the .jsp-view register and manages data access
	 * @return register
	 *  */
	@RequestMapping(value = "/register.html", method = RequestMethod.POST)
	public String processRegistrationForm(@Valid User user, BindingResult result) {
		// when user object has errors specified by hibernate annotations,
		// no user record is saved to the data base,
		// user stays on the registration page
		if (result.hasErrors()) {
			log.info("register failed");
			for (ObjectError error : result.getAllErrors()) {
				log.info(" - error:" + error.toString());
			}
			return "register";
		}
		log.info(result.hasErrors());
		// save user object as data base record and get temporal user object 
		User tmpUser = userDao.save(user);
		if (user.getId() == null) {
			user.setId(tmpUser.getId());
		}
		// redirect user to the login page
		return "login";
	}
	
	/**
	 * In case of Requesting "/register.html" & RequestMethod = GET,
	 * the Controller opens the .jsp-view register and manages data access
	 * @return register
	 *  */
	@RequestMapping(value = "/register.html", method = RequestMethod.GET)
	public String showRegistrationForm(ModelMap model) {
		// add to model attributes new empty user object 
		model.addAttribute("user", new User());
		// user stays on the register page
		return "register";
	}
	
	/**
	 * In case of Requesting "/home.html" & RequestMethod = GET,
	 * the Controller opens the .jsp-view home and manages data access
	 * @return home
	 *  */
	@RequestMapping(value = "/home.html", method = RequestMethod.GET)
	public String printWelcome(ModelMap model) {
		// get username of currently authentificated user
		Authentication auth = SecurityContextHolder.getContext().getAuthentication();
		String username = auth.getName();
		// set username of currently logged in user to the model attributes
		model.addAttribute("username", username);
		// user is on the home page
		return "home";
	}
	
	/**
	 * In case of Requesting "/login.html" & RequestMethod = GET,
	 * the Controller opens the .jsp-view login and manages data access
	 * @return login
	 *  */
	@RequestMapping(value = "/login.html", method = RequestMethod.GET)
	public String login(ModelMap model) {
		// return to user login page
		return "login";
	}
	
	/**
	 * In case of Requesting "/loginfailed.html" & RequestMethod = GET,
	 * the Controller opens the .jsp-view login and manages data access
	 * @return login
	 *  */
	@RequestMapping(value = "/loginfailed.html", method = RequestMethod.GET)
	public String loginerror(ModelMap model) {
		// set to the model attributes flag, that login is failed
		model.addAttribute("error", "true");
		// user stays on the login page
		return "login";
	}
	
	/**
	 * In case of Requesting "/j_spring_security_logout" & RequestMethod = GET,
	 * the Controller opens the .jsp-view login and manages data access
	 * @return login
	 *  */
	@RequestMapping(value = "/j_spring_security_logout", method = RequestMethod.GET)
	public String logout(ModelMap model) {
		// after logout user is on the login page 
		return "login";
	}

}
